package com.tarena.lbs.test.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * es索引操作
 */
public class ESIndexTestRun {
    //使用@Before构建一个使用的客户端对象
    private RestHighLevelClient client;
    @Before
    public void initClient(){
        //构造一个可以连接本地localhost:9200的client对象
        HttpHost host=new HttpHost("localhost",9200,"http");
        RestClientBuilder builder = RestClient.builder(host);
        client=new RestHighLevelClient(builder);
    }

    //创建索引 相当于在数据库生成创建一个 库
    @Test
    public void createIndex() throws IOException {
        //1 es提供了一个创建索引的http接口 put请求 client中几乎对所有的接口功能都封装**Request对象
        CreateIndexRequest request=new CreateIndexRequest("test01");//提供索引名称
        //2 构造完调用功能的请求对象 根据需求 向请求request填写补充一些请求参数 或头 发起请求
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //3 从响应中能看到 创建索引的结果
        System.out.println("创建索引："+response.isAcknowledged());
    }
    //指定索引创建的条件 分布式分片结构数据 n个分片 n个副本 就是在请求中request添加请求参数
    @Test
    public void createShardIndex() throws IOException {
        CreateIndexRequest request=new CreateIndexRequest("test02");
        //让test02索引 设置5个分片 0个副本
        Settings.Builder setting = Settings.builder()
                .put("number_of_shards", 5)
                .put("number_of_replicas", 0);
        request.settings(setting);
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println("创建索引："+response.isAcknowledged());
    }
    //删除索引
    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request=new DeleteIndexRequest("test03");
        client.indices().delete(request,RequestOptions.DEFAULT);
    }

    //指定索引创建的条件 mapping映射 设置自定义字段属性特性
    @Test
    public void createMappingIndex() throws IOException {
        CreateIndexRequest request=new CreateIndexRequest("test03");
        //给请求添加一个settings分片设置
        Settings.Builder setting =
                Settings.builder()
                        .put("number_of_shards", 1)
                        .put("number_of_replicas", 0);
        request.settings(setting);
        //设置添加一个mapping
        String mapping="{\"properties\":{\"name\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\"},\"student_no\":{\"type\":\"keyword\"},\"birthday\":{\"type\":\"date\"},\"location\":{\"type\":\"geo_point\"},\"gender\":{\"type\":\"keyword\"}}}";
        request.mapping(mapping, XContentType.JSON);
        client.indices().create(request,RequestOptions.DEFAULT);
    }
    //创建的索引，默认没有mapping映射，索引中的doc文档属性是自动生成映射数据
    //判断指定索引名称是否存在
    @Test
    public void existsIndex() throws IOException {
        GetIndexRequest request=new GetIndexRequest("test03");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    }


}
